#!/bin/bash  
# chkconfig: 2345 10 90  
# description: Nagios to ElasticSearch Service ....  
  
EXEC_PATH=@ng_dir@
EXEC=ngpull
DAEMON=$EXEC_PATH/$EXEC  
PID_FILE=$EXEC_PATH/var/$EXEC.pid  
ServiceName='NgPull'  
lockfile=/var/lock/subsys/$EXEC
SERVICE_LOG=$EXEC_PATH/var/$EXEC.log

. /etc/rc.d/init.d/functions  
  
if [ ! -x $DAEMON ] ; then  
       echo "ERROR: $DAEMON not found"  
       exit 1  
fi  
  
stop()  
{  
    echo -n "Stoping $ServiceName: "  
    if [ -w $EXEC_PATH/liaoyq ]; then
       mv $EXEC_PATH/liaoyq  $EXEC_PATH/var/liaoyq
       retval=$?
    else
       ps aux | grep "$DAEMON" | kill -9 `awk '{print $2}'` >/dev/null 2>&1 
       retval=$?
       sleep 1
    fi
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}  
  
start()  
{  
       echo -n "Starting $ServiceName: "  
       pid=`pidofproc -p $PID_FILE $EXEC`
       if [ -n "$pid" ] ; then
          echo "Already running."
          exit 0
       else
           if [ -w $EXEC_PATH/var/liaoyq ]; then
                mv $EXEC_PATH/var/liaoyq $EXEC_PATH/liaoyq
           else
                echo "liaoyq" > $EXEC_PATH/liaoyq
           fi
           daemon $DAEMON 1>"$SERVICE_LOG" 2>&1 &
           RETVAL=$?
           sleep 2
           pidofproc $EXEC > $PID_FILE
           [[ $? -eq 0 ]] && success || failure
           echo
           [ $RETVAL -eq 0 ] && touch $lockfile
           return $RETVAL
       fi
}  
restart()  
{  
    stop  
    start  
}  
  
case "$1" in  
    start)  
        start  
        ;;  
    stop)  
        stop  
        ;;  
    restart)  
        restart  
        ;;  
    status)  
        status -p $PID_FILE $DAEMON  
        ;;  
    *)  
        echo "Usage: service $ServiceName {start|stop|restart|status}"  
        exit 1  
esac  
  
exit $?
